const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const fs = require('fs');
const dayjs = require('dayjs');
const staticPath = 'static';
const staticDomain = '/public/';
const appVersion = dayjs().format('YYYYMMDDHHmmss') //app版本，用时间字符串记录

module.exports = {
    outputDir: 'dist/' + staticPath,
    indexPath: '../index.html',
    publicPath: process.env.NODE_ENV === 'production' ? staticDomain + staticPath + '/' : '/',
    productionSourceMap: false,
    devServer: {
        host: '0.0.0.0',
        port: 7002,
        proxy: {
            //开发环境接口
            '/apiI18n': {
                target: 'https://lang.daydao.com',
                changeOrigin: true,
                secure: false,
                pathRewrite: {
                    '^/apiI18n': ''
                }
            },

            //本地接口
            '/apiILocal18n': {
                // target: 'http://127.0.0.1:7001',
                target: 'http://192.168.3.241:7001', //开发生产环境
                changeOrigin: true,
                secure: false,
                pathRewrite: {
                    '^/apiILocal18n': ''
                }
            }
        }
    },
    chainWebpack(config) {
        //生成index.html中的环境变量
        config.plugin('html').tap((args) => {
            // 版本号生成
            args[0].currentAppVersion = appVersion

            // 环境变量生成
            const argnArr = process.argv
            let currentEnv = 'dev' // 默认dev环境
            argnArr.forEach(item => {
                if (item.indexOf('--env=') !== -1) {
                    currentEnv = item.replace('--env=', '')
                }
            })
            args[0].currentEnv = currentEnv
            return args
        })
    },
    configureWebpack: config => {
        config.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
        config.plugins.push(
            //解决moment打包的时候把所有的语言都打包进去的问题
            new webpack.ContextReplacementPlugin(
                /moment[\\/]locale$/,
                /^\.\/(zh-cn)$/
            )
        )

        if (process.env.NODE_ENV === 'production') {
            // 为生产环境修改配置...
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true; //去掉console
            config.plugins.push(
                //清理目录
                new CleanWebpackPlugin(['dist'], {
                    root: path.resolve(__dirname, './'),
                    verbose: true
                })
            )

            //生成版本文件，用于检测pda的升级
            const infoJson =  {
                appVersion: appVersion,
                updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
            }
            const content = JSON.stringify(infoJson);

            //指定创建目录及文件名称，__dirname为执行当前js文件的目录
            const file = path.join(__dirname, 'public/version.json');

            //写入文件
            fs.writeFile(file, content, function(err) {
                if (err) {
                    return console.log(err);

                }
                console.log('文件创建成功，地址：' + file);
            });

        }else{
            config.devtool = 'cheap-source-map'
        }
    },
    css: {
        loaderOptions: {
            sass: {
                prependData: '@import "@/style/var.scss";'
            }
        }
    }
}
